package com.company.controller;

import com.company.constant.MessageConstant;
import com.company.entity.PageResult;
import com.company.entity.QueryPageBean;
import com.company.entity.Result;
import com.company.pojo.CheckItem;
import com.company.service.CheckItemService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author
 * @date 2021-08-03 19:06
 * 关于检查项的增删改查的方法
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @DubboReference //查询服务
    private CheckItemService checkItemService;

    /**
     * @date  增加检查项
     * @param checkItem
     * @return com.company.entity.Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //服务失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }



    /**
     * @date  分页查询
     * @param queryPageBean 前端参数
     * @return PageResult  分页结果封装对象
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkItemService.pageQuery(
                                queryPageBean.getCurrentPage(),
                                queryPageBean.getPageSize(),
                                queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * @date  删除注册项
     * @param id
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result deleteCheckItem(Integer id){
        try {
            checkItemService.deleteCheckItem(id);
        } catch (Exception e) {
            //删除失败
            e.printStackTrace();
            //服务调用失败    Result是对于新增操作结果的封装
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * @date  修改注册项
     * @param checkItem
     */
    @RequestMapping("/edit")
    public Result editCheckItem(@RequestBody CheckItem checkItem){
        try {
            checkItemService.editCheckItem(checkItem);
        } catch (Exception e) {
            //删除失败
            e.printStackTrace();
            //服务调用失败    Result是对于新增操作结果的封装
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * @date  根据Id查询注册项
     * @param id 注册项 ID
     */
    @RequestMapping("/findById")
    public Result findByIdCheckItem(Integer id){
        CheckItem checkItem=null;
        try {
            checkItem =  checkItemService.findByIdCheckItem(id);
        } catch (Exception e) {
            //删除失败
            e.printStackTrace();
            //服务调用失败    Result是对于新增操作结果的封装
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
    }
}
